import { Provide, Inject } from '@midwayjs/decorator';
import { BaseRpcService, CoolRpcService } from '@cool-midway/rpc';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { WebsiteUserEntity } from '../entity/websiteUser';
import { ILogger } from '@midwayjs/logger';
import { Utils } from '../comm/utils';
/**
 * 描述
 */
@Provide()
@CoolRpcService({
  entity: WebsiteUserEntity,
  method: ['add', 'delete', 'update', 'info', 'list', 'page'],
})
export class WebsiteUserService extends BaseRpcService {
  @InjectEntityModel(WebsiteUserEntity)
  WebsiteUserEntity: Repository<WebsiteUserEntity>;
  @Inject()
  logger: ILogger;
  @Inject()
  utils: Utils;
  async list(params?: any): Promise<any> {
    if (!params.websiteId) {
      throw new Error('网站ID不能为空');
    }
    return this.nativeQuery(`
     SELECT
        tpw.id AS id,
        tpw.website_name AS websiteName,
        tpw.website_url AS websiteUrl,
        tpw.website_icon AS websiteIcon,
        tpw.website_desc AS websiteDesc,
        twu.user_id AS userId
      FROM
        t_website_user twu
        LEFT JOIN t_plugin_website tpw ON twu.website_id=tpw.id
        WHERE tpw.id=${params.websiteId}
      ORDER BY id DESC
      `);
  }
}
